package p270jp.pxv.android.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.lang.Thread;
import java.util.List;
import p270jp.pxv.android.R;
import p270jp.pxv.android.legacy.model.PixivUgoiraFrame;
import p274k2.ContextCompat;
import p577vl.UgoiraCache;

/* renamed from: jp.pxv.android.view.UgoiraView */
/* loaded from: classes2.dex */
public class UgoiraView extends SurfaceView implements SurfaceHolder.Callback {

    /* renamed from: a */
    public int f19705a;

    /* renamed from: c */
    public final SurfaceHolder f19707c;

    /* renamed from: d */
    public long f19708d;

    /* renamed from: e */
    public List<PixivUgoiraFrame> f19709e;

    /* renamed from: f */
    public int f19710f = 0;

    /* renamed from: b */
    public C4394b f19706b = new C4394b(null);

    /* renamed from: jp.pxv.android.view.UgoiraView$b */
    /* loaded from: classes2.dex */
    public class C4394b extends Thread {

        /* renamed from: a */
        public int f19711a;

        /* renamed from: b */
        public boolean f19712b = false;

        /* renamed from: c */
        public final Object f19713c = new Object();

        /* renamed from: d */
        public int f19714d;

        /* renamed from: e */
        public int f19715e;

        public C4394b(C4393a aVar) {
        }

        /* renamed from: a */
        public final void m4458a(Canvas canvas, Bitmap bitmap) {
            int i = this.f19714d;
            int i2 = this.f19715e;
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            float f = (float) height;
            float f2 = f / ((float) i2);
            float f3 = (float) width;
            float f4 = f3 / ((float) i);
            float f5 = f2 > f4 ? 1.0f / f2 : 1.0f / f4;
            int round = Math.round(f3 * f5);
            int round2 = Math.round(f * f5);
            int i3 = (i - round) / 2;
            int i4 = (i2 - round2) / 2;
            Rect rect = new Rect(0, 0, width, height);
            Rect rect2 = new Rect(i3, i4, round + i3, round2 + i4);
            canvas.drawColor(UgoiraView.this.f19705a);
            canvas.drawBitmap(bitmap, rect, rect2, (Paint) null);
        }

        /* renamed from: b */
        public void m4457b(boolean z) {
            synchronized (UgoiraView.this.f19707c) {
                synchronized (this.f19713c) {
                    this.f19712b = z;
                    if (this.f19711a != 4) {
                        if (z) {
                            this.f19711a = 1;
                        } else {
                            this.f19711a = 3;
                        }
                    }
                }
            }
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            Canvas lockCanvas;
            long j;
            long j2;
            while (this.f19712b) {
                if (this.f19711a == 1) {
                    UgoiraView ugoiraView = UgoiraView.this;
                    if (!(ugoiraView.f19709e == null || (lockCanvas = ugoiraView.f19707c.lockCanvas(null)) == null)) {
                        synchronized (UgoiraView.this.f19707c) {
                            synchronized (this.f19713c) {
                                UgoiraCache aVar = UgoiraCache.f29156e;
                                UgoiraView ugoiraView2 = UgoiraView.this;
                                Bitmap c = aVar.m1111c(ugoiraView2.f19708d, ugoiraView2.f19710f);
                                long currentTimeMillis = System.currentTimeMillis();
                                if (c != null) {
                                    m4458a(lockCanvas, c);
                                    UgoiraView ugoiraView3 = UgoiraView.this;
                                    UgoiraView ugoiraView4 = UgoiraView.this;
                                    ugoiraView4.f19710f = (ugoiraView4.f19710f + 1) % ugoiraView4.f19709e.size();
                                    long currentTimeMillis2 = System.currentTimeMillis() - currentTimeMillis;
                                    long delay = ugoiraView3.f19709e.get(ugoiraView3.f19710f).getDelay();
                                    if (currentTimeMillis2 < delay) {
                                        j2 = delay - currentTimeMillis2;
                                    }
                                }
                                j2 = 0;
                            }
                        }
                        UgoiraView.this.f19707c.unlockCanvasAndPost(lockCanvas);
                        if (j2 > 0) {
                            long currentTimeMillis3 = System.currentTimeMillis();
                            for (j = 0; j < j2; j = System.currentTimeMillis() - currentTimeMillis3) {
                            }
                        }
                    }
                }
            }
        }
    }

    public UgoiraView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        SurfaceHolder holder = getHolder();
        this.f19707c = holder;
        holder.setFormat(-2);
        holder.addCallback(this);
        Context context2 = getContext();
        Object obj = ContextCompat.f19963a;
        this.f19705a = ContextCompat.C4473d.m4257a(context2, R.color.charcoal_white);
    }

    /* renamed from: a */
    public boolean m4461a() {
        int i = this.f19706b.f19711a;
        return i == 2 || i == 4;
    }

    /* renamed from: b */
    public void m4460b(boolean z) {
        if (z) {
            C4394b bVar = this.f19706b;
            synchronized (UgoiraView.this.f19707c) {
                bVar.f19711a = 4;
            }
            return;
        }
        C4394b bVar2 = this.f19706b;
        synchronized (UgoiraView.this.f19707c) {
            if (bVar2.f19711a == 1) {
                bVar2.f19711a = 2;
            }
        }
    }

    /* renamed from: c */
    public void m4459c(boolean z) {
        if (z) {
            C4394b bVar = this.f19706b;
            synchronized (UgoiraView.this.f19707c) {
                bVar.f19711a = 1;
            }
            return;
        }
        C4394b bVar2 = this.f19706b;
        synchronized (UgoiraView.this.f19707c) {
            if (bVar2.f19711a == 2) {
                bVar2.f19711a = 1;
            }
        }
    }

    public void setFrames(List<PixivUgoiraFrame> list) {
        this.f19709e = list;
    }

    public void setThumbnail(Bitmap bitmap) {
        C4394b bVar = this.f19706b;
        Canvas lockCanvas = UgoiraView.this.f19707c.lockCanvas(null);
        if (lockCanvas != null) {
            synchronized (UgoiraView.this.f19707c) {
                bVar.m4458a(lockCanvas, bitmap);
            }
            UgoiraView.this.f19707c.unlockCanvasAndPost(lockCanvas);
        }
    }

    public void setWorkId(long j) {
        this.f19708d = j;
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
        C4394b bVar = this.f19706b;
        synchronized (UgoiraView.this.f19707c) {
            bVar.f19714d = i2;
            bVar.f19715e = i3;
        }
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        if (this.f19706b.getState() == Thread.State.TERMINATED) {
            this.f19706b = new C4394b(null);
        }
        Canvas lockCanvas = this.f19707c.lockCanvas();
        if (lockCanvas != null) {
            synchronized (this.f19707c) {
                lockCanvas.drawColor(this.f19705a);
            }
            this.f19707c.unlockCanvasAndPost(lockCanvas);
        }
        this.f19706b.m4457b(true);
        this.f19706b.start();
    }

    @Override // android.view.SurfaceHolder.Callback
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        this.f19706b.m4457b(false);
        boolean z = true;
        while (z) {
            try {
                this.f19706b.join();
                z = false;
            } catch (InterruptedException unused) {
            }
        }
    }
}
